import Taro from "@tarojs/taro";
import { HTTP_STATUS_CODE } from "@/config/http-status-code";
import { isMock, mockList, mockUrl } from "@/config/mock-config";

console.log("process.env.NODE_ENV:", process.env.NODE_ENV);
let base =
  process.env.NODE_ENV === "development"
    ? "https://habitformat-season-5-api-api-1.test.xthktech.cn"
    : "https://habitformat-season-5-api-api-1.test.xthktech.cn";
export default {
  baseOptions(params, method = "GET") {
    let { url, data } = params;
    isMock && mockList.includes(url) && (base = mockUrl);
    // let token = getApp().globalData.token
    // if (!token) login()
    // console.log('params', params)
    let token = Taro.getStorageSync("token");
    let contentType = "application/x-www-form-urlencoded";
    contentType = params.contentType || contentType;
    const option = {
      isShowLoading: false,
      loadingText: "正在加载",
      url: base + url,
      data: data ? JSON.parse(JSON.stringify(data)) : data,
      method: method,
      header: {
        "content-type": contentType,
        token: token
      }
    };
    return new Promise((resolve, reject) => {
      Taro.request(option)
        .then(res => {
          switch (res.statusCode) {
            case HTTP_STATUS_CODE.SUCCESS: {
              if (res.data.status_code === HTTP_STATUS_CODE.SUCCESS) {
                return resolve(res.data);
              } else if (
                ~~res.data.status_code === HTTP_STATUS_CODE.AUTHENTICATE
              ) {
                // console.log("登录失效");
                const oldToken = Taro.getStorageSync("token");
                oldToken && Taro.removeStorageSync("token");
                Taro.navigateTo({ url: "/pages/not-login/index" });
                return reject(res.data);
              } else if (
                res.data.status_code === HTTP_STATUS_CODE.ACTIVITY_DELETE
              ) {
                // 活动删除了跳首页
                Taro.showToast({
                  title: res.data.message,
                  icon: "none",
                  duration: 2000
                });
                setTimeout(() => {
                  Taro.switchTab({
                    url: "/pages/index/index"
                  });
                }, 2000);
                return reject(res.data);
              } else {
                return reject(res.data);
              }
            }
            case HTTP_STATUS_CODE.AUTHENTICATE:
              console.log("登录失效");
              /**移除token并跳转登录页 */
              const oldToken = Taro.getStorageSync("token");
              oldToken && Taro.removeStorageSync("token");
              Taro.navigateTo({ url: "/pages/not-login/index" });
              return reject(res.data);
            default:
              return reject(res.data);
          }
        })
        .catch(rej => {
          reject(rej.data);
        });
    });
  },
  get(url, data = "") {
    let option = { url, data };
    return this.baseOptions(option);
  },
  post: function(url, data, contentType) {
    let params = { url, data, contentType };
    return this.baseOptions(params, "POST");
  }
};
